import json
from selenium.webdriver.common.by import By as By
from selenium.common.exceptions import NoSuchElementException
import time

def input(self,data,type,index,num):
    try:
        input =  self.driver.find_element(type, data.iloc[index, 2])
    except NoSuchElementException:
        data.iloc[index, num-1] = 'error'
    else:
        if data.iloc[index, 3] != '':
            array = data.iloc[index, 3].split(',')
            if array[0] == 'url':  # 如果是url开头，则判断接口
                urlResponse(data,index,num,array[1])
            else:
                input.send_keys(array[1])

def click(self,data,type,index,num):
    try:
        button =  self.driver.find_element(type, data.iloc[index, 2])
    except NoSuchElementException:
        data.iloc[index, num-1] = 'error'
    else:
        button.click()
        time.sleep(1)
        if data.iloc[index, 3] != '':
            array = data.iloc[index, 3].split(',')
            if array[0] == 'url':  # 如果是url开头，则判断接口
                urlResponse(self,data,index,num,array[1])

def urlResponse(self,data,index,num,url):
    for request in self.driver.requests:
        if request.url.find(url) != -1:
            response = request.response
            response_body = json.loads(response.body.decode('utf-8'))
            if response.status_code != '200' and response_body["code"] != 0:
                data.iloc[index, num-1] = response_body["msg"]

                        
